package b2.widget.vip
{
	import b2.net.*;
	import b2.player.*;
	import b2.widget.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.text.*;
	import flash.ui.Mouse;

	public class VipDailyGift extends Sprite
	{
		private var presentBox1:Sprite;
		private var presentBox2:Sprite;
		private var emptyFlag:Boolean;
		private var newPlayerTitle:TextField;
		
		public function VipDailyGift()
		{
			super();
			//宝箱1
			var VipPresentBox1:Class = ResourceHelper.getInstance().getClassByName("VipPresentBox1");
			presentBox1 = new VipPresentBox1();
			addChild(presentBox1);
			//宝箱2
			var VipPresentBox2:Class = ResourceHelper.getInstance().getClassByName("VipPresentBox2");
			presentBox2 = new VipPresentBox2();
			presentBox2.visible = false;
			addChild(presentBox2);
			this.mouseChildren = false;
			//新手礼物
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			newPlayerTitle = new TextField();
			newPlayerTitle.selectable = false;
        	newPlayerTitle.autoSize = TextFieldAutoSize.LEFT;
        	newPlayerTitle.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x8F470E, true);
			newPlayerTitle.filters = [new GlowFilter(0xF9F744, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			newPlayerTitle.text = "黄钻新手";
			newPlayerTitle.x = -25;
			newPlayerTitle.y = 10;
			addChild(newPlayerTitle);
			newPlayerTitle.visible = !me.fetchNewPlayerGift;
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.CLICK, onMouseClick);
			ServerHelper.getInstance().addEventListener(ServerEvent.FETCH_NEW_PLAYER_GIFT, onFetchFinish);
		}
		
		private function onFetchFinish(event:ServerEvent):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.FETCH_NEW_PLAYER_GIFT, onFetchFinish);
			newPlayerTitle.visible = false;
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			//空礼盒
			if (emptyFlag)
			{
				var vipPrivilegesDialog:VipPrivilegesDialog = new VipPrivilegesDialog();
				WidgetManager.getInstance().popOut([vipPrivilegesDialog], true, true);
			}
			else
			{
				//新手礼物
				if (!me.fetchNewPlayerGift)
				{
					var vipNewPlayerDialog:VipNewPlayerDialog = new VipNewPlayerDialog();
					WidgetManager.getInstance().popOut([vipNewPlayerDialog], true, true);
					if (!(me.is_yellow_vip || me.is_yellow_year_vip))
					{
						setEmpty();
					}
				}
				//每日礼物
				else
				{
					ServerHelper.getInstance().fetchVipDailyPresent();
					setEmpty();
				}
			}
		}
		
		private function setEmpty():void
		{
			emptyFlag = true;
			presentBox1.visible = false;
			presentBox2.visible = true;
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
			var tipText:String;
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			if (emptyFlag)
			{
				tipText = "查看黄钻特权";
			}
			else
			{
				//新手礼物
				if (!me.fetchNewPlayerGift)
				{
					tipText = "领取新手礼物";
				}
				//每日礼物
				else
				{
					tipText = "领取每日礼物";
				}
			}
			var tip:BasicMessage = new BasicMessage(tipText);
			tip.x = globalPoint.x + this.width - 15;
			tip.y = globalPoint.y - 15;
			WidgetManager.getInstance().popOutMouseOverMessage(tip);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			WidgetManager.getInstance().closeMouseOverMessage();
		}
		
	}
}